<template>
  <div>
    <div>
      <el-input style="width: 200px; " placeholder="请输入标题" v-model="name"/>
      <el-button type="primary" @click="getTableData(1)" style="margin-left: 10px">搜索</el-button>
      <el-button type="primary" @click="resetData">重置</el-button>
    </div>
    <div style="margin: 5px 0px">
      <el-button type="primary" plain @click="addOrders">新增</el-button>
      <el-button type="danger" plain @click="delBatch">删除</el-button>
    </div>
    <el-table :data="tableData" stripe :header-cell-style="{background:'#df6e2c',color:'white'}"  @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"></el-table-column>
      <el-table-column prop="id" label="序号" align="center"></el-table-column>
      <el-table-column prop="no" label="订单编号" align="center"></el-table-column>
      <el-table-column prop="name" label="订单名称" align="center" show-overflow-tooltip></el-table-column>
      <el-table-column prop="money" label="订单金额" align="center"></el-table-column>
      <el-table-column prop="user" label="创建人" align="center"></el-table-column>
      <el-table-column prop="category" label="订单分类" align="center"></el-table-column>
      <el-table-column prop="date" label="更新日期" align="center"></el-table-column>
      <el-table-column label="操作" align="center" width="180">
        <template v-slot="scope">
          <el-button type="primary" size="mini" plain @click="editOrders(scope.row)">编辑</el-button>
          <el-button type="danger" size="mini" plain @click="del(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="block">
      <el-pagination
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-size="pageSize"
          layout="total, prev, pager, next"
          :total="total">
      </el-pagination>
    </div>

    <el-dialog title="订单信息" :visible="formVisible" width="40%" @close="closeDialog" >
      <el-form :model="form" label-width="80px" style="padding-right: 20px" :rules="rules" ref="VisibleRef">
        <el-form-item label="订单名称" prop="name" >
          <el-input v-model="form.name"  placeholder="标题"></el-input>
        </el-form-item>
        <el-form-item label="订单金额" prop="money" >
          <el-input type="" v-model="form.money"  placeholder="金额"></el-input>
        </el-form-item>
        <el-form-item label="订单类型" prop="category" >
          <el-select  v-model="form.category" style="width: 100%">
            <el-option v-for="item in ['水果','蔬菜','熟食','生鲜']" :key="item" :value="item">{{item}}</el-option>
          </el-select>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="formVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>

export default {
  name: "Notice",
  data(){
    return {
      ids:[],
      tableData:[],
      pageNum:1,  //当前页码数
      pageSize:5, //每页显示多少条数据
      total:0,
      name:'',
      formVisible:false,
      form:{},
      user:JSON.parse(localStorage.getItem('user') || '{}'),
      rules: {
        name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
        money: [{ required: true, message: '请输入金额', trigger: 'blur' }],
        category: [{ required: true, message: '请输入分类', trigger: 'blur' }],
      },
      content:''
    }
  },
  methods:{

    getTableData(pageNum){
      if (pageNum) this.pageNum=pageNum
      this.$request.get("/orders/selectByPage",{
       params:{
         pageNum:this.pageNum,
         pageSize:this.pageSize,
         name:this.name
       }
      }).then(res=>{　　　　
        this.tableData=res.data.records
        this.total=res.data.total
      })
    },
    handleCurrentChange(pageNum){
      this.getTableData(pageNum)
    },
    resetData(){
      this.name=''
      this.getTableData(1)
    },
    changeOpen(form){
      this.form = {...form}
      this.sendSaveRequest()
    },

    save(){
      this.$refs['VisibleRef'].validate(valid=>{
        if (valid){
          this.sendSaveRequest()
        }
      })
    },

    sendSaveRequest(){
      this.$request({
        url:this.form.id?'/orders/update':'/orders/add',
        method:"post",
        data:this.form
      }).then(res=>{
        if (res.code==='200'){
          this.$message.success('保存成功')
          this.getTableData()
          this.formVisible =false
        }else {
          this.$message.error(res.msg)
        }
      })
    },

    addOrders(){
      this.form={}
      this.formVisible=true
    },
    editOrders(notice){
      this.form= {...notice}
      this.formVisible=true
    },

    closeDialog(){
      this.formVisible=false
    },
    del(id) {
      this.$confirm("确定删除吗？","确定删除",{type:"warning"}).then(resp=>{
        this.$request.post('/orders/delete/'+id).then(res=>{
          if (res.code==='200'){
            this.$message.success("删除成功")
            this.getTableData(1)
          }else {
            this.$message.error(res.msg)
          }
        })
      }).catch(error => {})
    },
    handleSelectionChange(v){
      this.ids= v.map(res=>res.id)
    },
    delBatch(){
      if (this.ids.length<=0){
        this.$message.info("请选择您要删除的数据")
        return
      }
      this.$confirm("确定批量删除吗?","确认删除",{type:"warning"}).then(resp=>{
        this.$request.post("/orders/delete/batch",this.ids).then(res=>{
          if (res.code==='200'){
            this.$message.success("删除数据成功")
            this.getTableData(1)
          }else {
            this.$message.error("删除数据失败")
          }
        })
      }).catch(err=>{})
    }
  },
  mounted() {
    this.getTableData()
  }
}
</script>

<style>
/*解决content内容框的show-overflow-tooltip过长问题*/
.el-tooltip__popper{
  max-width: 200px !important;
}
</style>